package com.guofen.controller.hsmpy;

import com.guofen.pojo.HsmSmallWorm;
import com.guofen.service.hsmpy.SmallWormBreedingFeedService;
import com.guofen.util.result.Result;
import com.guofen.util.result.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/hsmpy/smallWormBreedingFeed")
@Controller
@Api(tags = "SmallWormBreedingFeedController", description = "1-2 小虫培育进料记录表")
public class SmallWormBreedingFeedController {

    @Autowired
    private SmallWormBreedingFeedService smallWormBreedingFeedService;

    @ApiOperation("增加记录")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Result add(@ApiParam(name = "id", value = "id", required = true) @RequestParam String id,
                      @ApiParam(name = "small_bug_breed_time", value = "加料日期（小虫培育开始日期，第二次、第三次加料日期）", required = true) @RequestParam String small_bug_breed_time,
                      @ApiParam(name = "small_bug_breed_material", value = "小虫培育料种类", required = true) @RequestParam String small_bug_breed_material,
                      @ApiParam(name = "small_bug_breed_material_weight", value = "小虫培育加料重量", required = true) @RequestParam String small_bug_breed_material_weight,
                      @ApiParam(name = "small_bug_breed_box_number", value = "小虫培育盒号", required = true) @RequestParam String small_bug_breed_box_number,
                      @ApiParam(name = "small_bug_breed_box_weight", value = "小虫培育盒重量", required = true) @RequestParam String small_bug_breed_box_weight,
                      @ApiParam(name = "small_bug_breed_box_remarks", value = "小虫加料备注", required = true) @RequestParam String small_bug_breed_box_remarks)

    {
        Integer Id = Integer.parseInt(id);
        Double small_bug_breed_material_w = Double.parseDouble(small_bug_breed_material_weight);
        Double small_bug_breed_box_w = Double.parseDouble(small_bug_breed_box_weight);
        if (smallWormBreedingFeedService.add(Id, small_bug_breed_time, small_bug_breed_material, small_bug_breed_material_w,small_bug_breed_box_number,small_bug_breed_box_w, small_bug_breed_box_remarks))
            return ResultGenerator.genSuccessResult();
        else
            return ResultGenerator.genFailResult("保存失败");
    }

    @ApiOperation("返回当天最新一条数据")
    @RequestMapping(value = "/getlatest", method = RequestMethod.GET)
    @ResponseBody
    public Result getlatest(){
        return ResultGenerator.genSuccessResult(smallWormBreedingFeedService.getlatest());
    }

    @ApiOperation("返回还未填写过1-2内容的n条数据，返回微虫培育未中止的数据")
    @RequestMapping(value = "/getlist", method = RequestMethod.GET)
    @ResponseBody
    public Result getlist(){
        return ResultGenerator.genSuccessResult(smallWormBreedingFeedService.getlist());
    }

    @ApiOperation("删除记录")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id) {
        return ResultGenerator.genSuccessResult(smallWormBreedingFeedService.delete(id));
    }

    @ApiOperation("修改记录")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Result update(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id,
                         @ApiParam(name="smallBugBreedStartTime",value="小虫培育开始日期",required=true) @RequestParam String smallBugBreedStartTime,
                         @ApiParam(name="batch",value="批次",required=true) @RequestParam String batch,
                         @ApiParam(name="wormGroup",value="组别",required=true) @RequestParam String wormGroup,
                         @ApiParam(name="hatchBoxNumber",value="孵化盒号",required=true) @RequestParam String hatchBoxNumber,
                         @ApiParam(name="smallBugBreedBoxNumber",value="小虫培育盒号",required=true) @RequestParam String smallBugBreedBoxNumber,
                         @ApiParam(name="smallBugBreedBoxWeight",value="小虫培育盒重量（kg）",required=true) @RequestParam Double smallBugBreedBoxWeight,
                         @ApiParam(name="smallBugBreedMaterialWeight",value="小虫培育加料重量1(kg)",required=true) @RequestParam Double smallBugBreedMaterialWeight,
                         @ApiParam(name="smallBugBreedMaterialWeight2",value="小虫培育加料重量2(kg)",required=true) @RequestParam Double smallBugBreedMaterialWeight2,
                         @ApiParam(name="smallBugBreedMaterialWeight3",value="小虫培育加料重量3(kg)",required=true) @RequestParam Double smallBugBreedMaterialWeight3) {
        HsmSmallWorm record = new HsmSmallWorm();
        record.setId(id);
        record.setSmallBugBreedStartTime(smallBugBreedStartTime);
        record.setBatch(batch);
        record.setWormGroup(wormGroup);
        record.setHatchBoxNumber(hatchBoxNumber);
        record.setSmallBugBreedBoxNumber(smallBugBreedBoxNumber);
        record.setSmallBugBreedBoxWeight(smallBugBreedBoxWeight);
        record.setSmallBugBreedMaterialWeight(smallBugBreedMaterialWeight);
        record.setSmallBugBreedMaterialWeight2(smallBugBreedMaterialWeight2);
        record.setSmallBugBreedMaterialWeight3(smallBugBreedMaterialWeight3);
        return ResultGenerator.genSuccessResult(smallWormBreedingFeedService.update(record));
    }

    @ApiOperation("查找记录")
    @RequestMapping(value = "/select",method = RequestMethod.POST)
    @ResponseBody
    public Result select(@ApiParam(name="pageSize",value="页容量",required=true) @RequestParam Integer pageSize,
                         @ApiParam(name="currentPage",value="当前页",required=true) @RequestParam Integer currentPage,
                         @ApiParam(name="startTime",value="开始时间") Long startTime,
                         @ApiParam(name="endTime",value="截止时间") Long endTime) {
        if (startTime == null || endTime == null)
            return ResultGenerator.genSuccessResult(smallWormBreedingFeedService.get(pageSize, currentPage));
        else
            return ResultGenerator.genSuccessResult(smallWormBreedingFeedService.getByTime(pageSize, currentPage, startTime, endTime));
    }
}
